package com.hmall.trade.listenr;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class PayStatusLisener {
    private final IOrderService orderService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("trade.pay.success.queue")
            ,exchange = @Exchange(value = "pay.direct")
            ,key = "pay.success"
    ))
    public void listenPaySuccess(Long orderId){
        //1、查询订单
        Order order = orderService.getById(orderId);
        //2、判断订单状态：是否已支付
        if(order==null|| order.getStatus()!=1){
            return;
        }
        //3、标记订单已支付

        orderService.markOrderPaySuccess(orderId);
    }
}
